<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\controller\shiyao;

use app\api\controller\Controller;
use app\api\model\shiyao\Crc as CenterCrcModel;
use app\api\model\shiyao\Pi as CenterPiModel;
use app\api\model\shiyao\ProjectTestee as TesteeModel;
use app\api\service\User as UserService;
use app\common\service\qrcode\Paper as PaperQRcode;
use cores\exception\BaseException;
use think\facade\Db;
use think\response\Json;

/**
 * 管理
 * Class
 * @package app\api\controller
 */
class Testee extends Controller {

	/**
	 * 列表
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function list(): Json {

		$user = UserService::getCurrentLoginUser(true);

		$role = $user['grade'] ? $user['grade']['grade_id'] : 0;

		if ($role == 2) {
			$model = new CenterPiModel;
			$list = $model->getList($this->request->param());
		} elseif ($role == 1) {
			$model = new CenterCrcModel;
			$list = $model->getList($this->request->param());
		} elseif ($role == 0) {

			$list = [];
		} else {
			//列表
			$model = new TesteeModel;
			$list = $model->getList($this->request->param());
		}

		return $this->renderSuccess(compact('list', 'role'));
	}

	/**
	 * 添加受试者
	 * @return Json
	 * @throws BaseException
	 */
	public function add(): Json {
		$model = new TesteeModel;
		if ($model->add($this->postForm())) {
			return $this->renderSuccess([], '添加成功');
		}
		return $this->renderError($model->getError() ?: '添加失败');
	}

	/**
	 * 详情
	 * @param int $testeeId 地址ID
	 * @return Json
	 * @throws BaseException
	 */
	public function qrcode(int $centerId, int $userId, $channel = 'MP-WEIXIN'): Json {
		// 创建二维码
		$Qrcode = new PaperQRcode($centerId, $userId, $channel);
		return $this->renderSuccess(['qrcode' => $Qrcode->getImage()]);
	}

	/**
	 * 详情
	 * @param int $testeeId 地址ID
	 * @return Json
	 * @throws BaseException
	 */
	public function detail(int $testeeId): Json {
		$detail = TesteeModel::detail($testeeId);
		return $this->renderSuccess(compact('detail'));
	}

	/**
	 * 编辑收货地址
	 * @param int $testeeId 地址ID
	 * @return Json
	 * @throws BaseException
	 */
	public function edit(): Json {
		$testeeId = $this->postForm()['testee_id'];
		$model = TesteeModel::detail($testeeId);

		//查询补助申请
		$log = Db::name('shiyao_project_pay_log')->where('testee_id', $testeeId)->find();
		if ($log) {
			return $this->renderError('有补助记录，无法修改');
		}

		if ($model->edit($this->postForm())) {
			return $this->renderSuccess([], '更新成功');
		}
		return $this->renderError($model->getError() ?: '更新失败');
	}

	/**
	 * 删除收货地址
	 * @param int $testeeId 地址ID
	 * @return Json
	 * @throws BaseException
	 */
	public function remove(int $testeeId): Json {

		$pay = Db::name('shiyao_project_pay_log')->where('testee_id', $testeeId)->find();
		if ($pay) {
			return $this->renderError('该受试者已申请补助，不可撤销');
		}

		$model = TesteeModel::detail($testeeId);
		if ($model->remove()) {
			return $this->renderSuccess([], '删除成功');
		}
		return $this->renderError($model->getError() ?: '删除失败');
	}
}
